package com.hongkzh.www.look.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.hongkzh.www.R;
import com.hongkzh.www.look.model.bean.CategoryBean;
import com.hongkzh.www.look.model.bean.CityAdvBean;
import com.hongkzh.www.look.model.bean.CityBean;
import com.hongkzh.www.look.model.bean.HotAdvBean;
import com.hongkzh.www.look.model.bean.StaticBean;
import com.hongkzh.www.other.banner.GlideImageLoader;
import com.hongkzh.www.other.recyclerview.Align;
import com.hongkzh.www.other.recyclerview.Config;
import com.hongkzh.www.other.recyclerview.StackLayoutManager;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asad on 2018/3/22.
 * 乐看 adapter
 */

public class LkRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements LkbRvVerAdapter.OnItemClickListener, LkrqRvAdapter.OnItemClickListener, LkhRvAdapter.OnClassifyClickListener, LkbRvHorAdapter.OnItemClickListener, LkcsRvHorAdapter.OnItemClickListener {
    private final int HEAD = 0;
    private final int CHENGSHI = 3;
    private final int RENQI = 5;
    private final int ITEM = 0;
    private final int ITEM1 = 1;
    private final int ITEM2 = 2;
    private final int ITEM3 = 3;
    private LkhRvAdapter lkhRvAdapter = new LkhRvAdapter();
    private LkrqRvAdapter lkrqRvAdapter = new LkrqRvAdapter();
    private LkcsRvAdapter lkcsRvAdapter = new LkcsRvAdapter();
    private List<String> title = new ArrayList<>();
    private List<String> carouselList = new ArrayList<>();
    private List<String> staticList = new ArrayList<>();
    @SuppressLint("UseSparseArrays")
    private Map<Integer, CityAdvBean> map = new HashMap<>();
    private int unit = 0;
    private Config config;
    private int slmposition;
    private View.OnClickListener onClickListener;

    public LkRvAdapter() {
        title.add("热门推荐");
        title.add("企业广告");
        title.add("自媒体");
        title.add("城市广告");
        title.add("传统文化");
        title.add("人气推荐");
    }

    @Override
    public int getItemViewType(int position) {
        if (position == HEAD)
            return ITEM;
        if (position == CHENGSHI)
            return ITEM1;
        if (position == RENQI)
            return ITEM2;
        return ITEM3;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        Context context = parent.getContext();
        if (viewType == ITEM) {
            view = LayoutInflater.from(context).inflate(R.layout.lk_head, parent, false);
            return new ViewHolder(view);
        } else if (viewType == ITEM1) {
            view = LayoutInflater.from(context).inflate(R.layout.lk_chengshi, parent, false);
            return new ViewHolder1(view);
        } else if (viewType == ITEM2) {
            view = LayoutInflater.from(context).inflate(R.layout.lk_renqi, parent, false);
            return new ViewHolder2(view);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.lk_body, parent, false);
            return new ViewHolder3(view);
        }
    }

    @SuppressLint("SetTextI18n")
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        switch (getItemViewType(position)) {
            case ITEM:
                final ViewHolder viewHolder = (ViewHolder) holder;
                GlideImageLoader glideImageLoader = new GlideImageLoader();
                Resources resources = holder.itemView.getContext().getResources();
                config = new Config();
                config.secondaryScale = 0.8f;
                config.scaleRatio = 0.8f;
                config.maxStackCount = 3;
                config.initialStackCount = 2;
                config.space = resources.getDimensionPixelOffset(R.dimen.x20);
                config.align = Align.RIGHT;
                unit = resources.getDimensionPixelOffset(R.dimen.x20) + resources.getDimensionPixelOffset(R.dimen.x280);
                viewHolder.lkhtTitle.setText(title.get(position));

                viewHolder.lkhtGengduo.setOnClickListener(onClickXiangQing(position));
                viewHolder.lkhtJiantou.setOnClickListener(onClickXiangQing(position));
                viewHolder.lkhImaLeft.setOnClickListener(onClickShiPin(position, 0));
                viewHolder.lkhtLeftZhu.setOnClickListener(onClickShiPin(position, 0));
                viewHolder.lkhtLeftFu.setOnClickListener(onClickShiPin(position, 0));
                viewHolder.lkhImaRight.setOnClickListener(onClickShiPin(position, 1));
                viewHolder.lkhtRightZhu.setOnClickListener(onClickShiPin(position, 1));
                viewHolder.lkhtRightFu.setOnClickListener(onClickShiPin(position, 1));
                viewHolder.lkhImaIcon.setOnClickListener(onClickJianJie(position, 0));
                viewHolder.lkbImaIcon.setOnClickListener(onClickJianJie(position));
                viewHolder.lkbTitleZhu.setOnClickListener(onClickShiPin(position));
                viewHolder.lkbTitleFu.setOnClickListener(onClickShiPin(position));

                viewHolder.lkhBan.setImageLoader(glideImageLoader).setImages(carouselList).setIndicatorGravity(BannerConfig.RIGHT).start();

                lkhRvAdapter.setOnClassifyClickListener(this);
                viewHolder.lkhRv.setLayoutManager(new GridLayoutManager(viewHolder.itemView.getContext(), 5, LinearLayoutManager.VERTICAL, false));
                viewHolder.lkhRv.setAdapter(lkhRvAdapter);

                StackLayoutManager stackLayoutManager = new StackLayoutManager(config);
                stackLayoutManager.setmUnit(unit);
                stackLayoutManager.setOnScrollListener(new StackLayoutManager.OnScrollListener() {
                    @Override
                    public void onScroll(int slmposition) {
                        LkRvAdapter.this.slmposition = slmposition;
                        Glide.with(viewHolder.lkbImaIcon).load(map.get(position).getData().get(slmposition).getHeadImg()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(viewHolder.lkbImaIcon);
                        viewHolder.lkbTitleZhu.setText(map.get(position).getData().get(slmposition).getTitle());
                        viewHolder.lkbTitleFu.setText(map.get(position).getData().get(slmposition).getImgNote());
                        viewHolder.bflTv.setText("" + map.get(position).getData().get(slmposition).getPlayCount());
                    }
                });
                if (map.get(position) != null) {
                    final List<CityAdvBean.DataBean> data = map.get(position).getData();
                    int size = data.size();
                    Glide.with(viewHolder.itemView).load(data.get(0).getCoverImgSrc()).into(viewHolder.lkhImaLeft);
                    Glide.with(viewHolder.itemView).load(data.get(0).getHeadImg()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(viewHolder.lkhImaIcon);
                    viewHolder.lkhtLeftZhu.setText(data.get(0).getTitle());
                    viewHolder.lkhtLeftFu.setText(data.get(0).getImgNote());
                    Glide.with(viewHolder.itemView).load(data.get(1).getCoverImgSrc()).into(viewHolder.lkhImaRight);
                    viewHolder.lkhtRightZhu.setText(data.get(1).getTitle());
                    viewHolder.lkhtRightFu.setText(data.get(1).getImgNote());
                    Glide.with(viewHolder.itemView).load(data.get(size - 1).getHeadImg()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(viewHolder.lkbImaIcon);
                    viewHolder.lkbTitleZhu.setText(data.get(0).getTitle());
                    viewHolder.lkbTitleFu.setText(data.get(0).getImgNote());
                    viewHolder.bflTv.setText("" + data.get(0).getPlayCount());
                    stackLayoutManager.setMposition(size - 1);
                    stackLayoutManager.setHuadongcd(unit * (size - 3));
                }
                LkbRvHorAdapter lkbRvHorAdapter = new LkbRvHorAdapter(map.get(position));
                lkbRvHorAdapter.setOnItemClickListener(this);
                viewHolder.lkbRvHor.setLayoutManager(stackLayoutManager);
                viewHolder.lkbRvHor.setAdapter(lkbRvHorAdapter);

                LkbRvVerAdapter lkbRvVerAdapter = new LkbRvVerAdapter(map.get(position));
                lkbRvVerAdapter.setOnItemClickListener(this);
                viewHolder.lkbRvVer.setLayoutManager(new GridLayoutManager(viewHolder.itemView.getContext(), 2, LinearLayoutManager.VERTICAL, false));
                viewHolder.lkbRvVer.setAdapter(lkbRvVerAdapter);

                viewHolder.lkhBanSta.setImageLoader(glideImageLoader).setImages(staticList).setIndicatorGravity(BannerConfig.RIGHT).start();
                break;
            case ITEM1:
                ViewHolder1 viewHolder1 = (ViewHolder1) holder;
                viewHolder1.lkhtTitle.setText(title.get(position));

                viewHolder1.lkhtGengduo.setOnClickListener(onClickXiangQing(position));
                viewHolder1.lkhtJiantou.setOnClickListener(onClickXiangQing(position));

                StackLayoutManager stackLayoutManager1 = new StackLayoutManager(config);
                stackLayoutManager1.setmUnit(unit);
                if (map.get(position) != null) {
                    final List<CityAdvBean.DataBean> data = map.get(position).getData();
                    int size = data.size();
                    stackLayoutManager1.setMposition(size - 1);
                    stackLayoutManager1.setHuadongcd(unit * (size - 3));
                }
                LkcsRvHorAdapter lkcsRvHorAdapter = new LkcsRvHorAdapter(map.get(position));
                lkcsRvHorAdapter.setOnItemClickListener(this);
                viewHolder1.lkcsRvHor.setLayoutManager(stackLayoutManager1);
                viewHolder1.lkcsRvHor.setAdapter(lkcsRvHorAdapter);

                viewHolder1.lkcsRv.setLayoutManager(new GridLayoutManager(viewHolder1.itemView.getContext(), 5, LinearLayoutManager.VERTICAL, false));
                viewHolder1.lkcsRv.setAdapter(lkcsRvAdapter);

                lkbRvVerAdapter = new LkbRvVerAdapter(map.get(position));
                lkbRvVerAdapter.setOnItemClickListener(this);
                viewHolder1.lkcsRvVer.setLayoutManager(new GridLayoutManager(viewHolder1.itemView.getContext(), 2, LinearLayoutManager.VERTICAL, false));
                viewHolder1.lkcsRvVer.setAdapter(lkbRvVerAdapter);
                break;
            case ITEM2:
                ViewHolder2 viewHolder2 = (ViewHolder2) holder;
                viewHolder2.lkhtTitle.setText(title.get(position));
                viewHolder2.lkhtGengduo.setVisibility(View.GONE);
                viewHolder2.lkhtJiantou.setVisibility(View.GONE);

                lkrqRvAdapter.setOnItemClickListener(this);
                viewHolder2.lkrqRv.setLayoutManager(new GridLayoutManager(viewHolder2.itemView.getContext(), 2, LinearLayoutManager.VERTICAL, false));
                viewHolder2.lkrqRv.setAdapter(lkrqRvAdapter);
                break;
            case ITEM3:
                final ViewHolder3 viewHolder3 = (ViewHolder3) holder;
                viewHolder3.lkhtTitle.setText(title.get(position));

                viewHolder3.lkhtGengduo.setOnClickListener(onClickXiangQing(position));
                viewHolder3.lkhtJiantou.setOnClickListener(onClickXiangQing(position));
                viewHolder3.lkbImaIcon.setOnClickListener(onClickJianJie(position));
                viewHolder3.lkbTitleZhu.setOnClickListener(onClickShiPin(position));
                viewHolder3.lkbTitleFu.setOnClickListener(onClickShiPin(position));

                StackLayoutManager stackLayoutManager3 = new StackLayoutManager(config);
                stackLayoutManager3.setmUnit(unit);
                if (map.get(position) != null) {
                    final List<CityAdvBean.DataBean> data = map.get(position).getData();
                    int size = data.size();
                    Glide.with(viewHolder3.lkbImaIcon).load(data.get(0).getHeadImg()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(viewHolder3.lkbImaIcon);
                    viewHolder3.lkbTitleZhu.setText(data.get(0).getTitle());
                    viewHolder3.lkbTitleFu.setText(data.get(0).getImgNote());
                    viewHolder3.bflTv.setText("" + data.get(0).getPlayCount());
                    stackLayoutManager3.setMposition(size - 1);
                    stackLayoutManager3.setHuadongcd(unit * (size - 3));
                    stackLayoutManager3.setOnScrollListener(new StackLayoutManager.OnScrollListener() {
                        @Override
                        public void onScroll(int slmposition) {
                            LkRvAdapter.this.slmposition = slmposition;
                            Glide.with(viewHolder3.lkbImaIcon).load(data.get(slmposition).getHeadImg()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(viewHolder3.lkbImaIcon);
                            viewHolder3.lkbTitleZhu.setText(data.get(slmposition).getTitle());
                            viewHolder3.lkbTitleFu.setText(data.get(slmposition).getImgNote());
                            viewHolder3.bflTv.setText("" + data.get(slmposition).getPlayCount());
                        }
                    });
                }
                lkbRvHorAdapter = new LkbRvHorAdapter(map.get(position));
                lkbRvHorAdapter.setOnItemClickListener(this);
                viewHolder3.lkbRvHor.setLayoutManager(stackLayoutManager3);
                viewHolder3.lkbRvHor.setAdapter(lkbRvHorAdapter);

                lkbRvVerAdapter = new LkbRvVerAdapter(map.get(position));
                lkbRvVerAdapter.setOnItemClickListener(this);
                viewHolder3.lkbRv.setLayoutManager(new GridLayoutManager(viewHolder3.itemView.getContext(), 2, LinearLayoutManager.VERTICAL, false));
                viewHolder3.lkbRv.setAdapter(lkbRvVerAdapter);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return title.size();
    }

    public void addCarouselList(StaticBean staticBean) {
        carouselList.clear();
        for (int i = 0; i < staticBean.getData().size(); i++) {
            carouselList.add(staticBean.getData().get(i).getImgSrc());
        }
    }

    public void addCategoryList(CategoryBean categoryBean) {
        lkhRvAdapter.add(categoryBean);
        lkhRvAdapter.notifyDataSetChanged();
    }

    public void addStaticList(StaticBean staticBean) {
        this.staticList.clear();
        for (int i = 0; i < staticBean.getData().size(); i++) {
            this.staticList.add(staticBean.getData().get(i).getImgSrc());
        }
    }

    public void addCityAdvList(Map<Integer, CityAdvBean> map) {
        this.map = map;
    }

    public void addCityList(CityBean cityBean) {
        lkcsRvAdapter.add(cityBean);
        lkcsRvAdapter.notifyDataSetChanged();
    }

    public void addHotAdvList(HotAdvBean hotAdvBean) {
        lkrqRvAdapter.add(hotAdvBean);
        lkrqRvAdapter.notifyDataSetChanged();
    }

    private View.OnClickListener onClickShiPin(final int position) {
        onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(map.get(position).getData().get(slmposition).getUid(), map.get(position).getData().get(slmposition).getId());
            }
        };
        return onClickListener;
    }

    private View.OnClickListener onClickShiPin(final int position, final int slmposition) {
        onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(map.get(position).getData().get(slmposition).getUid(), map.get(position).getData().get(slmposition).getId());
            }
        };
        return onClickListener;
    }

    private View.OnClickListener onClickJianJie(final int position) {
        onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onIconClickListener.onIconClick(map.get(position).getData().get(slmposition).getUid());
            }
        };
        return onClickListener;
    }

    private View.OnClickListener onClickJianJie(final int position, final int slmposition) {
        onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onIconClickListener.onIconClick(map.get(position).getData().get(slmposition).getUid());
            }
        };
        return onClickListener;
    }

    private View.OnClickListener onClickXiangQing(final int position) {
        onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onTiaoZhuanClickListener.onTiaoZhuanClick(position);
            }
        };
        return onClickListener;
    }

    public interface OnTiaoZhuanClickListener {
        void onTiaoZhuanClick(int position);
    }

    private OnTiaoZhuanClickListener onTiaoZhuanClickListener;

    public void setOnTiaoZhuanClickListener(OnTiaoZhuanClickListener onTiaoZhuanClickListener) {
        this.onTiaoZhuanClickListener = onTiaoZhuanClickListener;
    }


    public interface OnIconClickListener {
        void onIconClick(String uid);
    }

    private OnIconClickListener onIconClickListener;

    public void setOnIconClickListener(OnIconClickListener onIconClickListener) {
        this.onIconClickListener = onIconClickListener;
    }

    public interface OnClassifyClickListener {
        void onClassifyClick(int type);
    }

    private OnClassifyClickListener onClassifyClickListener;

    public void setOnClassifyClickListener(OnClassifyClickListener onClassifyClickListener) {
        this.onClassifyClickListener = onClassifyClickListener;
    }

    @Override
    public void onClassifyClick(int type) {
        onClassifyClickListener.onClassifyClick(type);
    }

    public interface OnItemClickListener {
        void onItemClick(String uid, String id);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onItemClick(String uid, String id) {
        onItemClickListener.onItemClick(uid, id);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.lkh_ban)
        Banner lkhBan;
        @BindView(R.id.lkh_rv)
        RecyclerView lkhRv;
        @BindView(R.id.lkht_title)
        TextView lkhtTitle;
        @BindView(R.id.lkht_gengduo)
        TextView lkhtGengduo;
        @BindView(R.id.lkht_jiantou)
        ImageView lkhtJiantou;
        @BindView(R.id.lkh_ima_left)
        ImageView lkhImaLeft;
        @BindView(R.id.lkh_ima_right)
        ImageView lkhImaRight;
        @BindView(R.id.lkh_ima_icon)
        ImageView lkhImaIcon;
        @BindView(R.id.lkht_left_zhu)
        TextView lkhtLeftZhu;
        @BindView(R.id.lkht_left_fu)
        TextView lkhtLeftFu;
        @BindView(R.id.lkht_right_zhu)
        TextView lkhtRightZhu;
        @BindView(R.id.lkht_right_fu)
        TextView lkhtRightFu;
        @BindView(R.id.lkb_rv_hor)
        RecyclerView lkbRvHor;
        @BindView(R.id.lkb_ima_icon)
        ImageView lkbImaIcon;
        @BindView(R.id.lkb_title_zhu)
        TextView lkbTitleZhu;
        @BindView(R.id.lkb_title_fu)
        TextView lkbTitleFu;
        @BindView(R.id.bfl_tv)
        TextView bflTv;
        @BindView(R.id.lkb_rv_ver)
        RecyclerView lkbRvVer;
        @BindView(R.id.lkh_ban_sta)
        Banner lkhBanSta;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.lkht_title)
        TextView lkhtTitle;
        @BindView(R.id.lkht_gengduo)
        TextView lkhtGengduo;
        @BindView(R.id.lkht_jiantou)
        ImageView lkhtJiantou;
        @BindView(R.id.lkcs_rv_hor)
        RecyclerView lkcsRvHor;
        @BindView(R.id.lkcs_rv)
        RecyclerView lkcsRv;
        @BindView(R.id.lkcs_rv_ver)
        RecyclerView lkcsRvVer;

        ViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder2 extends RecyclerView.ViewHolder {
        @BindView(R.id.lkht_title)
        TextView lkhtTitle;
        @BindView(R.id.lkht_gengduo)
        TextView lkhtGengduo;
        @BindView(R.id.lkht_jiantou)
        ImageView lkhtJiantou;
        @BindView(R.id.lkrq_rv)
        RecyclerView lkrqRv;

        ViewHolder2(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder3 extends RecyclerView.ViewHolder {
        @BindView(R.id.lkht_title)
        TextView lkhtTitle;
        @BindView(R.id.lkht_gengduo)
        TextView lkhtGengduo;
        @BindView(R.id.lkht_jiantou)
        ImageView lkhtJiantou;
        @BindView(R.id.lkb_rv_hor)
        RecyclerView lkbRvHor;
        @BindView(R.id.lkb_ima_icon)
        ImageView lkbImaIcon;
        @BindView(R.id.lkb_title_zhu)
        TextView lkbTitleZhu;
        @BindView(R.id.lkb_title_fu)
        TextView lkbTitleFu;
        @BindView(R.id.bfl_tv)
        TextView bflTv;
        @BindView(R.id.lkb_rv_ver)
        RecyclerView lkbRv;

        ViewHolder3(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
